package com.tangyee.person_risk_assessment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tangyee.person_risk_assessment.StartRiskAssessmentActivity;
import com.tangyee.R;
import com.tangyee.person_risk_assessment.StartRiskAssessmentActivity;

import java.util.List;

public class OptionsListAdapter extends BaseAdapter {

    private Context context;
    private ListView listView;
    private List<String> optionList;

    private int checkedPosition;

    private boolean isFinalQuestion;

    /**
     *
     * @param context
     * @param optionList
     * @param listView
     * @param preTimeCheckedPosition 上一次这题所做的选项的位置
     */
    public OptionsListAdapter(Context context, List<String> optionList, ListView listView, int preTimeCheckedPosition){
        this.context = context;
        this.optionList = optionList;
        this.listView = listView;
        this.checkedPosition = preTimeCheckedPosition;
        this.isFinalQuestion = false;
    }

    public int getCheckedPosition() {
        return checkedPosition;
    }

    public void setCheckedPosition(int lastTimeCheckedPosition) {
        this.checkedPosition = lastTimeCheckedPosition;
    }

    @Override
    public int getCount() {
        return optionList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public boolean isFinalQuestion() {
        return isFinalQuestion;
    }

    public void setFinalQuestion(boolean finalQuestion) {
        isFinalQuestion = finalQuestion;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View optionView = LayoutInflater.from(context).inflate(R.layout.layout_option, null);
        TextView textView = optionView.findViewById(R.id.option_text);
        textView.setText(optionList.get(i));

        if(!isFinalQuestion()) {
            TextView checkedImage = optionView.findViewById(R.id.checked_image);
            setVisible(i, checkedImage);
        }else {
            /**
             * 身高与体重是每次必填的项，每次评估都不需考虑上一次评估结果
             */
            TextView choiceText = optionView.findViewById(R.id.choice_text);
            if (i == 0 && StartRiskAssessmentActivity.userQuestionHeight != -1){ // 设置身高
                choiceText.setText(StartRiskAssessmentActivity.userQuestionHeight + " m");
                choiceText.setVisibility(View.VISIBLE);
            }else if(i == 1 && StartRiskAssessmentActivity.userQuestionWeight != -1) { // 设置体重
                choiceText.setText(StartRiskAssessmentActivity.userQuestionWeight + " kg");
                choiceText.setVisibility(View.VISIBLE);
            }
        }

        return optionView;
    }

    private void setVisible(int position, TextView view) {
        if (listView.isItemChecked(position)) {
            // 点击了item 使得图片的可见发生改变
            view.setVisibility(View.VISIBLE);
        }else if(checkedPosition == position){
            /**
             * 没有点击item 加载使得图片发生改变
             * （1、上一次评估点击这道题的状态
             *  2、这次评估这个fragment 的item的点击位置）翻页回来
             */
            view.setVisibility(View.VISIBLE);
        }
    }


}
